package com.bogu.activity;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bogu.adapter.DynamicAdapter;
import com.bogu.constant.ConstantS;
import com.bogu.dao.BiologyDao;
import com.bogu.model.Comment;
import com.bogu.model.Dynamic;
import com.bogu.view.XListView;
import com.bogu.view.XListView.IXListViewListener;
import com.example.boguplatform.R;
import com.xuan.tuya.BasicActivity;
import com.xuan.tuya.InjectView;

public class DynamicActivity extends BasicActivity {
	@InjectView(R.id.hint)
	private TextView hint;
	@InjectView(R.id.dynamiclist)
	private XListView list;
	@InjectView(R.id.progressBar)
	private ProgressBar progressBar;
	private BiologyDao biologyDao;
	private ArrayList<Dynamic> dynamics;
	private SharedPreferences preferences;
	private DynamicAdapter dynamicAdapter;
	private DisplayMetrics dm;
	private int m = 0;
	private int count;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO 自动生成的方法存根
			switch (msg.what) {
			case 0:
				progressBar.setVisibility(View.VISIBLE);
				hint.setVisibility(View.GONE);
				break;
			case 1:
				progressBar.setVisibility(View.GONE);
				switch (m) {
				case 0:
					dynamics = (ArrayList<Dynamic>) msg.obj;
					count = msg.arg1;
					dynamicAdapter = new DynamicAdapter(dynamics,
							DynamicActivity.this, dm,false);
					list.setAdapter(dynamicAdapter);
				
					if (count <= 10) {
						if(count==0){
							hint.setVisibility(View.VISIBLE);
							
						}
						list.setPullLoadEnable(false);

					}

					break;
				case 1:
					ArrayList<Dynamic> dynamics0 = (ArrayList<Dynamic>) msg.obj;

					dynamics0.addAll(dynamics);
					dynamics = dynamics0;
					dynamicAdapter.setDynamics(dynamics);
                   
					dynamicAdapter.notifyDataSetChanged();
                    if(dynamics.size()==0){
                    	hint.setVisibility(View.VISIBLE);
                    }
					list.stopRefresh();

					break;
				case 2:

					dynamics.addAll((ArrayList<Dynamic>) msg.obj);
					dynamicAdapter.setDynamics(dynamics);
					dynamicAdapter.notifyDataSetChanged();
					list.stopLoadMore();
					if (((ArrayList<Dynamic>) msg.obj).size() <= 10) {
						list.setPullLoadEnable(false);
					}
					break;
				}
				break;
			case 2:
				progressBar.setVisibility(View.GONE);
				//list.stopRefresh();
				list.stopLoadMore();
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dynamic);
		registerBroadcastReceiver();
		initActionBar();
		dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		preferences = getSharedPreferences(ConstantS.KEY_SETTING,
				Context.MODE_PRIVATE);
		biologyDao = new BiologyDao(this, handler);
		biologyDao.getDynamic(preferences.getInt(ConstantS.KEY_UID, 0), 0, "");
		list.setPullRefreshEnable(true);
		list.setPullLoadEnable(true);
		list.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				// TODO 自动生成的方法存根
				
				if (dynamics.size() > 0) {
					m=1;
					biologyDao.getDynamic(preferences.getInt(ConstantS.KEY_UID,
							0), dynamics.get(0).getId(), "refresh");
				} else {
					m=0;
					biologyDao.getDynamic(
							preferences.getInt(ConstantS.KEY_UID, 0), 0, "");
				}
			}

			@Override
			public void onLoadMore() {
				// TODO 自动生成的方法存根
				m = 2;
    
				biologyDao.getDynamic(preferences.getInt(ConstantS.KEY_UID, 0),
						dynamics.get(dynamics.size() - 1).getId(), "loadmore");

			}
		});
	}
	public void registerBroadcastReceiver() {
		IntentFilter myIntentFilter = new IntentFilter();

		myIntentFilter.addAction("refresh_dynamic");

		// 注册广播
		registerReceiver(mBroadcastReceiver, myIntentFilter);

	}

	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals("refresh_dynamic")) {
				System.out.println("send");
				if (dynamics.size() > 0) {
					m=1;
					biologyDao.getDynamic(preferences.getInt(ConstantS.KEY_UID,
							0), dynamics.get(0).getId(), "refresh");
				} else {
					m=0;
					biologyDao.getDynamic(
							preferences.getInt(ConstantS.KEY_UID, 0), 0, "");
				}
				
			}

		}

	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO 自动生成的方法存根
		getMenuInflater().inflate(R.menu.dynamic, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO 自动生成的方法存根
		switch (arg1) {
		case RESULT_OK:
			Bundle b = arg2.getExtras();
			int position = b.getInt("position");		    
			dynamics.get(position).setComment_count(dynamics.get(position).getComment_count()+b.getInt("plus"));
			dynamicAdapter.notifyDataSetChanged();
			list.setAdapter(dynamicAdapter);
			break;
		
		}

	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO 自动生成的方法存�?
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		case R.id.get:
			m=0;
			biologyDao.getDynamic(preferences.getInt(ConstantS.KEY_UID, 0), 0, "");
			
			break;
		case R.id.share:
			Intent intent=new Intent(DynamicActivity.this,EditDynamicActivity.class);
			startActivity(intent);
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void initActionBar() {

		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(true);
		getSupportActionBar().setDisplayUseLogoEnabled(false);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayShowCustomEnabled(true);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setTitle("分享");
	}
	@Override
	protected void onDestroy() {
		// TODO 自动生成的方法存根
		super.onDestroy();
		unregisterReceiver(mBroadcastReceiver);
	}
	
}
